import axios from 'axios'
import store from '@/store'

const instance = axios.create({
  timeout: 1000 * 10,
  baseURL: process.env.NODE_ENV === 'development' ? 'https://app.wpengtao.com:8222' : 'https://app.wpengtao.com:8222'
  // baseURL: process.env.NODE_ENV === 'development' ? 'http://localhost:8089' : 'http://localhost:8089'
});

//请求配置
instance.interceptors.request.use(
  config => {
    store.dispatch('setLoading', true);
    // if (config.method == 'post') {
      // config.data = qs.stringify(config.data);
    // }
    return config;
  }
)

//响应配置
instance.interceptors.response.use(
  res => {
    setTimeout(() => {
      store.dispatch('setLoading', false);
    }, 500);
    if (res.data.code !== '200') {
      return Promise.reject(res.data);
    }
    return res.data;
  },
  error => {
    setTimeout(() => {
      store.dispatch('setLoading', false);
    }, 500);
    return Promise.reject(error);
  }
)

const post = (url, data, config = {}) => instance.post(url, data, config);

const get = (url, params, config = {}) =>
  instance.get(url, {
    params,
    ...config
  });

export default {
  install(Vue) {
    Vue.prototype.$reqGet = get;
    Vue.prototype.$reqPost = post;
  }
};